<?php

namespace app\store\controller\wanlshop;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;

/**
 * 商品管理
 *
 * @icon fa fa-circle-o
 */
class Treatment extends Backend
{

    /**
     * Goods模型对象
     * @var \app\store\model\wanlshop\Goods
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\store\model\wanlshop\Treatment;
        $tree = Tree::instance();
        // 1.0.2升级 过滤隐藏
        $tree->init(model('app\index\model\wanlshop\Category')->where(['type' => 'goods', 'isnav' => 1])->field('id,pid,name')->order('weigh asc,id asc')->select());
        $this->assignconfig('channelList', $tree->getTreeArray(0));
        $this->view->assign("flagList", $this->model->getFlagList());
        $this->view->assign("channelList", $this->model->getChannelList());
        $this->view->assign("stockList", $this->model->getStockList());
        $this->view->assign("specsList", $this->model->getSpecsList());
        $this->view->assign("distributionList", $this->model->getDistributionList());
        $this->view->assign("activityList", $this->model->getActivityList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("groundingList", $this->model->getGroundingList());


    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['category', 'shopsort', 'shop'])
                ->where($where)
                ->where('gdtype', '=', 1)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['category', 'shopsort', 'shop'])
                ->where($where)
                ->where('gdtype', '=', 1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {
                $row->getRelation('category')->visible(['name']);
                $row->getRelation('shopsort')->visible(['name']);
                $row->getRelation('shop')->visible(['shopname']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 选择链接
     */
    public function select()
    {
        if ($this->request->isAjax()) {
            return $this->index();
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        //设置过滤方法
        $this->request->filter(['']);
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                // 判断产品属性是否存在
                empty($params['spuItem']) ? $this->error(__('请完善：销售信息 - 产品属性')) : '';
                $result = false;
                Db::startTrans();
                try {

                    $spudata = isset($params['spu']) ? $params['spu'] : $this->error(__('请填写销售信息-产品属性'));
                    $spuItem = isset($params['spuItem']) ? $params['spuItem'] : $this->error(__('请填写销售信息-产品属性-产品规格'));
                    // 获取自增ID
                    //  $this->model->shop_id = $this->shop->id;
                    $this->model->shop_id = 1;
                    //  $this->model->brand_id = $params['brand_id'];
                    $this->model->category_id = $params['category_id'];
                    if (isset($params['attribute'])) {
                        $this->model->category_attribute = json_encode($params['attribute'], JSON_UNESCAPED_UNICODE);
                    }
                    $this->model->gdtype = 1;
                    $this->model->title = $params['title'];
                    $this->model->image = $params['image'];
                    $this->model->images = $params['images'];
                    $this->model->description = $params['description'];
                    $this->model->stock = $params['stock'];
                    $this->model->code = $params['code'];
                    $this->model->unit = $params['unit'];
                    $this->model->tags = $params['tags'];
                    $this->model->flag = $params['flag'];
                    $this->model->grounding = $params['grounding'];
                    $this->model->channel = $params['channel'];
//	                $this->model->status = $params['status'];
                    $this->model->content = $params['content'];
                    $this->model->video = $params['video'];
//	                $this->model->shop_category_id = $params['shop_category_id'];
                    $this->model->price = min($params['price']);
                    $this->model->freight_id = $params['freight_id'];
                    if ($this->model->save()) {
                        $result = true;
                    }
                    // 写入SPU
                    $spu = [];
                    foreach (explode(",", $spudata) as $key => $value) {
                        $spu[] = [
                            'goods_id' => $this->model->id,
                            'name'     => $value,
                            'item'     => $spuItem[$key]
                        ];
                    }
                    if (!model('app\index\model\wanlshop\GoodsSpu')->allowField(true)->saveAll($spu)) {
                        $result == false;
                    }
                    // 写入SKU
                    $sku = [];
                    foreach ($params['sku'] as $key => $value) {
                        $sku[] = [
                            'goods_id'      => $this->model->id,
                            'thumbnail'     => isset($params['thumbnail']) ? $params['thumbnail'][$key] : false, // 1.0.8升级
                            'difference'    => $value,
                            'code'          => $params['skucode'][$key],
                            'cost_price'    => $params['cost_price'][$key], // 1.0.8升级
                            'channel_price' => $params['channel_price'][$key], // 1.0.8升级
//                            'market_price'  => $params['market_price'][$key],
                            'price'         => $params['price'][$key],
                            'stock'         => $params['stocks'][$key],
//                            'weigh'         => $params['weigh'][$key] != '' ? $params['weigh'][$key] : 0,
                            'warn_stock'   => $params['warn_stock'][$key],
                            'sn'            => $params['skucode'][$key]
                        //    'sn'            => $params['sn'][$key] != '' ? $params['sn'][$key] : 'wanl_' . time()
                        ];
                    }
                    if (!model('app\index\model\wanlshop\GoodsSku')->allowField(true)->saveAll($sku)) {
                        $result == false;
                    }
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $shop_id = 1;//$this->shop->id;
        // 判断是否存在品牌
        $row['brand'] = model('app\index\model\wanlshop\Brand')->where(['state' => 1])->count();
        // 判断是否有店铺分类
        $row['shopsort'] = model('app\index\model\wanlshop\ShopSort')->where('shop_id', $shop_id)->count();
        // 判断是否有运费模板
        $row['freight'] = model('app\index\model\wanlshop\ShopFreight')->where('shop_id', $shop_id)->count();
        // 判断是否有寄件人信息
        $row['config'] = model('app\index\model\wanlshop\ShopConfig')->where('shop_id', $shop_id)->find();
        // 打开方式
        $this->assignconfig("isdialog", IS_DIALOG);
        $this->view->assign("row", $row);

        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        //设置过滤方法
        $this->request->filter(['']);
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

//        if ($row['shop_id'] != $this->shop->id) {
//            $this->error(__('You have no permission'));
//        }
        // 查询SKU
        $skuItem = model('app\index\model\wanlshop\GoodsSku')
            ->where(['goods_id' => $ids, 'state' => 0])
            ->field('id,thumbnail,difference,price,market_price,stock,weigh,sn,sales,state,cost_price,channel_price,code as skucode,warn_stock')
            ->select();
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                // 判断产品属性是否存在
                empty($params['spuItem']) ? $this->error(__('请完善：销售信息 - 产品属性')) : '';
                $result = false;
                Db::startTrans();
                try {
                    $spudata = isset($params['spu']) ? $params['spu'] : $this->error(__('请填写销售信息-产品属性'));
                    $spuItem = isset($params['spuItem']) ? $params['spuItem'] : $this->error(__('请填写销售信息-产品属性-产品规格'));
                    // 写入表单
                    $data = $params;
                    if (isset($data['attribute'])) {
                        $data['category_attribute'] = json_encode($data['attribute'], JSON_UNESCAPED_UNICODE);
                    }
                    $data['price'] = min($data['price']);
                    $result = $row->allowField(true)->save($data);
                    // 删除原来数据,重新写入SPU
                    model('app\index\model\wanlshop\GoodsSpu')
                        ->where('goods_id', 'in', $ids)
                        ->delete();
                    $spu = [];
                    foreach (explode(",", $spudata) as $key => $value) {
                        $spu[] = [
                            'goods_id' => $ids,
                            'name'     => $value,
                            'item'     => $spuItem[$key]
                        ];
                    }
                    if (!model('app\index\model\wanlshop\GoodsSpu')->allowField(true)->saveAll($spu)) {
                        $result == false;
                    }
                    //标记旧版SKU数据
                    $oldsku = [];
                    foreach ($skuItem as $value) {
                        $oldsku[] = [
                            'id'    => $value['id'],
                            'state' => 1
                        ];
                    }
                    if (!model('app\index\model\wanlshop\GoodsSku')->allowField(true)->saveAll($oldsku)) {
                        $result == false;
                    }
                    // 写入SKU
                    $sku = [];
                    foreach ($params['sku'] as $key => $value) {
                        $sku[] = [
                            'goods_id'     => $ids,
                            'thumbnail'    => isset($params['thumbnail']) ? $params['thumbnail'][$key] : false, // 1.0.8升级
                            'difference'   => $value,
                            'code'          => $params['skucode'][$key],
                            'cost_price'    => $params['cost_price'][$key], // 1.0.8升级
                            'channel_price' => $params['channel_price'][$key], // 1.0.8升级
//                            'market_price' => $params['market_price'][$key],
                            'price'        => $params['price'][$key],
                            'stock'        => $params['stocks'][$key],
//                            'weigh'        => $params['weigh'][$key] != '' ? $params['weigh'][$key] : 0,
                            'warn_stock'   => $params['warn_stock'][$key],
                            'sn'            => $params['skucode'][$key]
                         //   'sn'           => $params['sn'][$key] != '' ? $params['sn'][$key] : 'wanl_' . time()
                        ];
                    }
                    if (!model('app\index\model\wanlshop\GoodsSku')->allowField(true)->saveAll($sku)) {
                        $result == false;
                    }
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $spuData = model('app\index\model\wanlshop\GoodsSpu')->all(['goods_id' => $ids]);
        $suk = [];
        foreach ($skuItem as $vo) {
            $suk[] = explode(",", $vo['difference']);
        }
        $spu = [];
        foreach ($spuData as $vo) {
            $spu[] = $vo['name'];
        }
        $spuItem = [];
        foreach ($spuData as $vo) {
            $spuItem[] = explode(",", $vo['item']);
        }
        $skulist = [];
        foreach ($skuItem as $vo) {
            $skulist[$vo['difference']] = $vo;
        }
        $this->assignconfig('spu', $spu);
        $this->assignconfig('spuItem', $spuItem);
        $this->assignconfig('sku', $suk);
        $this->assignconfig('skuItem', $skulist);
        $this->assignconfig('categoryId', $row['category_id']);
        $this->assignconfig('attribute', json_decode($row['category_attribute']));
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 添加类目属性
     */
    public function attribute()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->request("id");
            // 1.0.8升级  获取父级类目属性
            $tree = Tree::instance();
            $tree->init(collection(model('app\index\model\wanlshop\Category')->select())->toArray(), 'pid');
            $list = model('app\index\model\wanlshop\Attribute')
                ->where('category_id', 'in', $tree->getParentsIds($id, true))
                ->where('status', 'normal')
                ->select();
            $this->success('查询成功', '', $list);
        }
        $this->error(__('Parameter %s can not be empty', ''));
    }

}
